﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Tracing;
using WebApiSelfHost.Filters;
using WebApiSelfHost.Handlers;

namespace WebApiSelfHost
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            // Web API configuration and services                      
            config.MapHttpAttributeRoutes();

            // Web API routes
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.SuppressHostPrincipal();

            // Web API message handler
            config.MessageHandlers.Add(new MessageRequestHandler());
            config.MessageHandlers.Add(new SessionIdHandler());

            // Web API filters
            config.Filters.Add(new UserAuthorizationFilter());
            config.Filters.Add(new EnableETag());
            config.Filters.Add(new ExceptionFilter());

            // Web API trace writer
            config.Services.Replace(typeof(ITraceWriter), new TraceWriter());
        }
    }
}
